package handler

import (
	"fmt"
	"io"
	"net/http"
	"time"
)

func GetData(url string, timeOut int) (string, error) {
	// 创建一个自定义的 http.Client
	client := &http.Client{
		Timeout: time.Duration(timeOut) * time.Millisecond, // 设置超时时间为200毫秒
	}
	resp, err := client.Get(url)
	if err != nil {
		fmt.Println("Error fetching data error is ", err)
		return "", fmt.Errorf("Error fetching data from %s: %v", url, err)
	}
	defer resp.Body.Close()

	body, err := io.ReadAll(resp.Body)
	if err != nil {
		fmt.Println("Error reading response body error is ", err)
		return "", fmt.Errorf("Error reading response body: %v", err)
	}

	return string(body), nil
}
